﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class apps_wlog_wlogList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            System.Web.Security.MembershipUser m = System.Web.Security.Membership.GetUser();
            this.ddlResources.Items.Clear();
            ListItemCollection llii = cUIUtili.getResourceComboItem(m.UserName);
            foreach (ListItem li in llii)
            {
                this.ddlResources.Items.Add(li);
            }
            //this.ddlResources.SelectedIndex = this.ddlResources.Items.IndexOf(this.ddlResources.Items.FindByText(m.UserName));
            this.TextBoxAnno.Text = System.DateTime.Now.Year.ToString();
            this.DropDownListMese.SelectedIndex = (System.DateTime.Now.Month - 1);
        }
        this.Repeater1.DataSource = getDataList(new Guid(this.ddlResources.SelectedValue),int.Parse(this.TextBoxAnno.Text),(this.DropDownListMese.SelectedIndex + 1));
        this.Repeater1.DataMember = "TABWL";
        this.Repeater1.DataBind();
    }
    private System.Data.DataTable getDataList(Guid resId, int a, int m)
    {
        System.Data.DataTable dt = new System.Data.DataTable("TABWL");
        string sSql = "SELECT [WorkLogId], [DayDate], [JobId], [JobTaskId], [WorkLogNote], [NumberOfUnit], [UnitMeasures], [ResourceId], [Status] FROM [WorkLog] WHERE [ResourceId] = ? AND Month([DayDate]) = ? AND Year([DayDate]) = ?;";
        sSql = "SELECT WorkLog.WorkLogId, WorkLog.DayDate, WorkLog.JobId, WorkLog.NumberOfUnit, WorkLog.UnitMeasures, WorkLog.ResourceId, WorkLog.Status, '[' + Trim([Jobs].[JobCode]) + '] ' + Trim([Jobs].[Description]) AS Job FROM (WorkLog LEFT OUTER JOIN Jobs ON WorkLog.JobId = Jobs.JobId) WHERE [WorkLog].[ResourceId] = ? AND Month([WorkLog].[DayDate]) = ? AND Year([WorkLog].[DayDate]) = ?;";
        try
        {
            System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand(sSql, cn);
            cm.Parameters.Add("ResourceId", System.Data.OleDb.OleDbType.Guid);
            cm.Parameters.Add("Month", System.Data.OleDb.OleDbType.Integer);
            cm.Parameters.Add("Year", System.Data.OleDb.OleDbType.Integer);
            cm.Parameters["ResourceId"].Value = resId;
            cm.Parameters["Month"].Value = m;
            cm.Parameters["Year"].Value = a;
            cn.Open();
            System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(cm);
            da.Fill(dt);
            da.Dispose();
            cn.Close();
        }
        catch (System.Data.OleDb.OleDbException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw e;
        }
        return dt;
    }
}
